﻿using UnityEngine;
using System.Collections;

public class EndLevelStat : MonoBehaviour {

    
    private int enemyKilledNb;
    public int EnemyKilledNb
    {
        get { return enemyKilledNb; }
        set { enemyKilledNb = value; }
    }

    private Level level;
    public Level Level
    {
        get { return level; }
        set { level = value; }
    }
    
    private int lostLifes;
    public int LostLifes
    {
        get { return lostLifes; }
        set { lostLifes = value; }
    }

    private float speedRatio;
    public float SpeedRatio
    {
        get { return speedRatio; }
        set { speedRatio = value; }
    }

    private bool win;
    public bool Win
    {
        get { return win; }
        set { win = value; }
    }


    private float enemyRatio;
    private float totalRatio;

    private float speedRatioBonus;
    private int levelEnemyCount;
    private int experience;
    public float ExperienceBonusRatio { get; set; }
    private int levelReward;

    private Rect backToMainMenuRect;
    private Rect resumeButtonRect;
    private Rect backGroundRect;

    private int widthOffset;
    private int heightOffset;

    public EndLevelStat()
    {
        ExperienceBonusRatio = 0;
        speedRatioBonus = 0.1f;
        widthOffset = 25;
        heightOffset = 25;
        resumeButtonRect = new Rect(Screen.width / 2 - 30, Screen.height - heightOffset - 50, 75, 40);
        backToMainMenuRect = new Rect(Screen.width - widthOffset - 25, heightOffset, 25, 25);
        backGroundRect = new Rect(widthOffset, heightOffset, Screen.width - 2 * widthOffset, Screen.height - 2 * heightOffset);
    }

    public void setStat(int enemyKilled, int lostLifes, bool win, float speedRatio, Level level)
    { 
        SpeedRatio = speedRatio;
        this.Level = level;
        EnemyKilledNb = enemyKilled;
        LostLifes = lostLifes;
        Win = win;

        //Calculate xp bonus
        enemyKilledNb = EnemyKilledNb;
        levelEnemyCount = Level.getTotalEnemyNumber();
        enemyRatio = (float)enemyKilledNb / (float)levelEnemyCount;

        SpeedRatio *= speedRatioBonus;

        totalRatio = enemyRatio + SpeedRatio + ExperienceBonusRatio;

        levelReward = Level.XpReward;
        experience = (int)(levelReward * totalRatio);
    }

    public bool printEndLevelGUI()
    {
        GUI.Box(backGroundRect, new GUIContent("", "Box hover"));
        GUI.Label(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 25, 200, 50), "Killed enemies : " + enemyKilledNb + "/" + levelEnemyCount);
        GUI.Label(new Rect(Screen.width / 2 + 100, Screen.height / 2 - 25, 200, 50), (int)(enemyRatio * 100) + "%");
        GUI.Label(new Rect(Screen.width / 2 - 100, Screen.height / 2, 200, 50), "+ Speed bonus : ");
        GUI.Label(new Rect(Screen.width / 2 + 100, Screen.height / 2, 200, 50), (int)(SpeedRatio * 100) + "%");
        GUI.Label(new Rect(Screen.width / 2 - 100, Screen.height / 2 + 25, 200, 50), "+ Upgrade bonus : ");
        GUI.Label(new Rect(Screen.width / 2 + 100, Screen.height / 2 + 25, 200, 50), (int)(ExperienceBonusRatio * 100) + "%");
        GUI.Label(new Rect(Screen.width / 2 - 100, Screen.height / 2 + 75, 200, 50), "XP gain : ");
        GUI.Label(new Rect(Screen.width / 2 + 100, Screen.height / 2 + 75, 200, 50), (int)(totalRatio * 100) + "%" + " x " + levelReward + "xp");
        if (GUI.Button(backToMainMenuRect, "X") || GUI.Button(resumeButtonRect, "Resume"))
        {
            EventManager<int, Vector3>.Raise("xp gain", experience, Camera.main.ScreenToWorldPoint(new Vector3(backToMainMenuRect.x, backToMainMenuRect.y, 0)));
            return false;
        }
        return true;
    }
}
